package servlet;

import Dbase.dataBase;
import model.userData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "servlet_login",urlPatterns = "/Pages/login")
public class servlet_login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        int id = Integer.parseInt(req.getParameter("id"));
        String pwd = req.getParameter("password");

//        out.println("id:"+id+"pwd:"+pwd);

        try {
            PrintWriter out = resp.getWriter();
            dataBase db = new dataBase();
            db.showConnected();
            userData userData = db.Login(id,pwd);
            if(userData == null){
                out.println("用户不存在或密码错误"+"<a href=\"http://localhost:8080/page_war_exploded/\">返回登陆</a>");
            }
            else{
                out.println("欢迎登陆"+userData.getName());
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

}
